<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in menuList">
                <template v-if="item.subs.length > 0">
                    <el-submenu :index="item.path" :key="item.id">
                        <template #title>
                            <i :class="item.icon"></i>
                            <span>{{ item.title }}</span>
                        </template>
	                    <el-menu-item v-for="subItem in item.subs" :index="subItem.path" :key="subItem.id">{{ subItem.title }}
	                    </el-menu-item>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.path" :key="item.id">
                        <i :class="item.icon"></i>
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script setup>
import { computed, ref } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
import {getMyMenu} from "../api/api";
const menuList = ref([])
//获取路径
const getMenu = () => {
	getMyMenu({token:localStorage.getItem('admin_token')}).then(res => {
		menuList.value = res.data
	}).catch(err => {
		console.log(err)
	})
}
getMenu()
//当前路由
const route = useRoute();
const onRoutes = computed(() => {
	return route.path;
});
//是否折叠
const store = useStore();
const collapse = computed(() => store.state.collapse);
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
	z-index: 1;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
	scrollbar-width: none;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
